package com.kunya.product.service.impl;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.AddressMapper;
import com.kunya.product.po.Address;
import com.kunya.product.service.IAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhangjiaqing
 * @Description:个人中心收货地址的接口的实现
 * @date 2018/7/17 19:05
 */
@Service("iAddressService")
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * @Author WangGang
     * @Description 新增收货地址
     * @Date 9:00 2018/7/18
     * @Param [userId, address, tablePrefix]
     **/
    @Override
    public ServerResponse insertAddress(Long userId, Address address, String tablePrefix) {
        if(userId==null || address==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        address.setCreateTime(new Date());
        address.setUserId(userId);
        return addressMapper.insertSelective(address,tablePrefix)==1?
                ServerResponse.createBySuccessMessage("新建地址成功"):ServerResponse.createByErrorMessage("新建地址失败");

    }
    /**
     * @Author WangGang
     * @Description 根据用户id查询收货地址列表
     * @Date 10:19 2018/7/18
     * @Param [userId, tablePrefix]
     **/
    @Override
    public ServerResponse<List<Address>> selectAddressListByUserId(Long userId, String tablePrefix) {
        if(userId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        List<Address> addressList = addressMapper.selectAddressListByUserId(userId,tablePrefix);
        return addressList==null?
                ServerResponse.createByErrorMessage("查询收货地址列表失败"):ServerResponse.createBySuccess(addressList);
    }

    /**
     * @Author WangGang
     * @Description 根据收货地址id和用户id查询收货地址详情
     * @Date 10:55 2018/7/18
     * @Param [addressId, userId, tablePrefix]
     **/
    @Override
    public ServerResponse selectAddress(Long addressId, Long userId, String tablePrefix) {
        if(addressId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        Address address = addressMapper.selectAddress(addressId,userId,tablePrefix);
        return address==null?
                ServerResponse.createByErrorMessage("查询失败"):ServerResponse.createBySuccess(address);
    }

    /**
     * @Author WangGang
     * @Description 根据收货地址id编辑收货地址
     * @Date 13:41 2018/7/18
     * @Param [addressId, address, tablePrefix]
     **/
    @Override
    public ServerResponse updateAddress(Long addressId, Address address, String tablePrefix) {
        if(addressId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        address.setUpdateTime(new Date());
        return addressMapper.updateByPrimaryKeySelective(address,tablePrefix)==1?
                ServerResponse.createBySuccess("修改地址成功"):ServerResponse.createByErrorMessage("修改地址失败");

    }

    /**
     * @param
     * @author zhangjiaqing
     * @Description: 删除收货地址
     * @date 2018/7/18 9:26
     */
    @Override
    public ServerResponse removeAddress(Long addressId, String tablePrefix) {
        if (addressId == null || StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        int removeResoult = addressMapper.deleteByPrimaryKey(addressId, tablePrefix);
        return removeResoult == 1 ?
                ServerResponse.createBySuccess("删除收货地址成功") : ServerResponse.createByErrorMessage("删除收货地址失败！");
    }

    /**
     * @param
     * @author zhangjiaqing
     * @Description: 设置默认收货地址
     * @date 2018/7/18 9:28
     */
    @Override
    public ServerResponse<Address> setDefaultAddress(Long userId, Long addressId, Boolean defaults, String tablePrefix) {
        if (userId == null || addressId == null || defaults == null || StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        int defaultResult = addressMapper.setDefaultAddress(userId, addressId, defaults, tablePrefix);
        if (defaultResult > 0) {
            Address address = addressMapper.selectByPrimaryKey(addressId, tablePrefix);
            return ServerResponse.createBySuccess("默认地址设置成功！", address);
        } else {
            return ServerResponse.createByErrorMessage("默认地址设置失败！");

        }
    }

}
